package com.ssfk.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class Placeholder {

    public final static String JDBCTYPE = "jdbcType";
    public final static String REQUIRED = "required";
    public final static String ISARRAY = "isArray";
    public final static String VARCHAR = "varchar";
    public final static String INTEGER = "integer";
    public final static String ENCRYPTOR = "encryptor"; // 加密

    protected static final Logger logger = LoggerFactory
            .getLogger(Placeholder.class);

    private String name;
    private String type;
    private String placeholder;
    private String encryptor;

    private boolean varchar;
    private boolean required;
    private boolean isArray;

    public boolean isArray() {
        return isArray;
    }

    public void setArray(boolean isArray) {
        this.isArray = isArray;
    }

    public boolean isRequired() {
        return required;
    }

    public void setRequired(boolean required) {
        this.required = required;
    }

    public boolean isVarchar() {
        return varchar;
    }

    public void setVarchar(boolean varchar) {
        this.varchar = varchar;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String jdbcType) {
        this.type = jdbcType;
    }

    public String getPlaceholder() {
        return placeholder;
    }

    public void setPlaceholder(String placeholder) {
        this.placeholder = placeholder;
    }

    public void setProperty(String expr) {
        Pattern pattern = Pattern.compile("((\\w+) *= *(\\w+))");
        Matcher matcher = pattern.matcher(expr);

        while (matcher.find()) {
            String propName = matcher.group(2);
            String propValue = matcher.group(3);
            if (JDBCTYPE.equalsIgnoreCase(propName)) {
                setType(propValue.toLowerCase());
                if (VARCHAR.equalsIgnoreCase(type)) {
                    setVarchar(true);
                }
            } else if (REQUIRED.equalsIgnoreCase(propName)) {
                setRequired(String.valueOf(true).equalsIgnoreCase(propValue));
            } else if (ISARRAY.equalsIgnoreCase(propName)) {
                this.setArray(String.valueOf(true).equalsIgnoreCase(propValue));
            } else if (ENCRYPTOR.equalsIgnoreCase(propName)) {
                this.setEncryptor(propValue);
            }
        }
    }

    public String getEncryptor() {
        return encryptor;
    }

    public void setEncryptor(String encryptor) {
        this.encryptor = encryptor;
    }

    /**
     * @param expr
     * @return List<Placeholder> 返回类型
     * @throws
     * @Title : createPlaceholders
     * @Description : 创建用于替换的变量
     * @params
     */
    public static Map<String, Placeholder> createPlaceholders(String expr,
                                                             boolean required) {
        Map<String, Placeholder> placeholders = new HashMap<>();

        if (null == expr || expr.isEmpty()) {
            logger.warn("Empty expr create placeholders");
            return placeholders;
        }

        Pattern pattern = Pattern.compile("(\\$\\{(\\w+)(,[^\\}.]*)*\\})");
        Matcher matcher = pattern.matcher(expr);
        while (matcher.find()) {
            Placeholder placeholder = new Placeholder();

            placeholder.setName(matcher.group(2));
            placeholder.setPlaceholder(matcher.group(1));
            if (null != matcher.group(3)) {
                placeholder.setProperty(matcher.group(3));
            }
            if (required) {
                placeholder.setRequired(required);
            }
            placeholders.put(placeholder.getName(), placeholder);
        }
        return placeholders;
    }
}